package seamaphore;

import java.util.concurrent.Semaphore;

/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2022-08-08
 * Time: 14:20
 */
//模拟车辆进入小区，通行证只有两张，有一辆车通行证就进入一辆，其他没有的则等待，进入车辆后就发放一个通行证
public class MyRunnable implements Runnable {
    //1.获得管理员对象，
    private final Semaphore semaphore=new Semaphore(2);
    
    @Override
    public void run() {
        //2.获得通行证
        try {
            semaphore.acquire();
            //3.开始行驶
            System.out.println("获得了通行证，开始行驶");
            Thread.sleep(500);
            System.out.println("归还通行证");
            //4.归还通行证
            semaphore.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
